# Author: HZT
# Date: 2023/8/28
import unittest
from config.config_loader import config
from page.web_sk_top import WebSkTop
from utils import UtilsDriver, login_with_session, get_db_ye, get_cid_by_username, GetLog
from page.web_sk_zl import WebSkZl
from utils import DBUtil
import datetime
import calendar


class TestWebSkZl(unittest.TestCase):
    driver = None

    @classmethod
    def setUpClass(cls) -> None:
        cls.log = GetLog.get_log()
        cls.driver = UtilsDriver.get_web_driver()
        # 实例化
        cls.skZl = WebSkZl(cls.driver)
        cls.skTop = WebSkTop(cls.driver)
        # 打开网页
        cls.driver.get(config.URL)
        login_with_session('system2', cls.driver)

    @classmethod
    def tearDownClass(cls):
        # 在所有测试用例执行完毕后，关闭浏览器
        # UtilsDriver.get_web_driver().quit()
        pass

    # 公司名字显示正确
    def test01_sk_zl(self):
        # 获取公司名称-页面
        gsmc = self.skZl.sk_zl_get_gsmz()
        # 获取公司名称
        gsmc_1 = self.skTop.sk_top_get_gsmc_js().split()[0]
        # 断言
        self.assertIn(gsmc_1, gsmc)

    # 余额显示正确
    def test02_sk_zl(self):
        # 获取余额
        ye = self.skZl.sk_zl_get_ye()
        if '.' not in ye:
            # 如果没有小数点，添加两个零并返回
            ye = ye + '.00'
        # 获取数据库余额
        db_ye = get_db_ye(config.CREDENTIALS["username_sk"])
        # 断言
        self.log.info(db_ye)
        self.assertEqual(ye.replace(',', ''), db_ye)

    # 本日收卡总额
    def test03_sk_zl(self):
        # 获取公司ID
        cid = get_cid_by_username(config.CREDENTIALS["username_sk"])
        # 页面获取本日收卡总额
        d1 = self.skZl.sk_zl_get_d1()
        if d1 == '￥0.00':
            d1 = 'None'
        # 获取当前日期
        today = datetime.date.today()
        # 构造起始日期和结束日期
        start_date = today.strftime('%Y-%m-%d')
        end_date = (today + datetime.timedelta(days=1)).strftime('%Y-%m-%d')
        sql_query = f"""
        SELECT
          sum(`rmb_amount`) AS aggregate
        FROM
          `transactions`
        WHERE
          `company_id` = {cid}
          AND `created_at` BETWEEN '{start_date}' AND '{end_date}'
          AND `status` = 1
          AND `type` = 3
          AND `transactions`.`deleted_at` IS NULL;
        """
        # 数据库获取本日收卡总额
        d1_d = DBUtil.select_one(sql_query)
        self.assertEqual(d1.replace(',', '').replace("￥", ""), str(d1_d[0]))

    # 本月收卡总额
    def test04_sk_zl(self):
        # 获取公司ID
        cid = get_cid_by_username(config.CREDENTIALS["username_sk"])
        # 页面获取本月收卡总额
        d2 = self.skZl.sk_zl_get_d2()
        # 获取当前日期
        current_date = datetime.date.today()
        # 获取当前月份的起始日期和结束日期
        start_date = current_date.replace(day=1)
        # 获取当前月份的最后一天
        _, last_day = calendar.monthrange(current_date.year, current_date.month)
        end_date = current_date.replace(day=last_day)

        # 拼接起始日期和结束日期的字符串形式
        start_date_str = start_date.strftime('%Y-%m-%d')
        end_date_str = end_date.strftime('%Y-%m-%d')
        sql_query = f"""
        SELECT
          sum(`rmb_amount`) AS aggregate
        FROM
          `transactions`
        WHERE
          `company_id` = {cid}
          AND `created_at` BETWEEN '{start_date_str}' AND '{end_date_str}'
          AND `status` = 1
          AND `type` = 3
          AND `transactions`.`deleted_at` IS NULL;
        """
        # 数据库获取本日收卡总额
        d2_d = DBUtil.select_one(sql_query)
        self.assertEqual(d2.replace(',', '').replace("￥", ""), str(d2_d[0]))

    # 本日收卡总数
    def test05_sk_zl(self):
        # 获取公司ID
        cid = get_cid_by_username(config.CREDENTIALS["username_sk"])
        # 页面获取本日收卡总数
        d3 = self.skZl.sk_zl_get_d3()
        # 获取当前日期
        today = datetime.date.today()
        # 构造起始日期和结束日期
        start_date = today.strftime('%Y-%m-%d')
        end_date = (today + datetime.timedelta(days=1)).strftime('%Y-%m-%d')
        sql_query = f"""
        SELECT
          count(*) AS aggregate
        FROM
          `transactions`
        WHERE
          `company_id` = {cid}
          AND `created_at` BETWEEN '{start_date}'
          AND '{end_date}'
          AND `status` = 1
          AND `type` = 3
          AND `transactions`.`deleted_at` IS NULL;
        """
        # 数据库获取本日收卡总额
        d3_d = DBUtil.select_one(sql_query)
        self.assertEqual(d3, str(d3_d[0]))

    # 本月收卡总数
    def test06_sk_zl(self):
        # 获取公司ID
        cid = get_cid_by_username(config.CREDENTIALS["username_sk"])
        # 页面获取本月收卡总数
        d4 = self.skZl.sk_zl_get_d4()
        # 获取当前日期
        current_date = datetime.date.today()

        # 获取当前月份的起始日期和结束日期
        start_date = current_date.replace(day=1)
        # 获取当前月份的最后一天
        _, last_day = calendar.monthrange(current_date.year, current_date.month)
        end_date = current_date.replace(day=last_day)

        # 拼接起始日期和结束日期的字符串形式
        start_date_str = start_date.strftime('%Y-%m-%d')
        end_date_str = end_date.strftime('%Y-%m-%d')
        sql_query = f"""
        SELECT
          count(*) AS aggregate
        FROM
          `transactions`
        WHERE
          `company_id` = {cid}
          AND `created_at` BETWEEN '{start_date_str}'
          AND '{end_date_str}'
          AND `status` = 1
          AND `type` = 3
          AND `transactions`.`deleted_at` IS NULL;
        """
        # 数据库获取本日收卡总额
        d4_d = DBUtil.select_one(sql_query)
        self.assertEqual(d4, str(d4_d[0]))
